import {Component, OnInit} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {ModuleCodeEditComponent} from '../module-code-edit/module-code-edit.component';
import {BladeContainerBase} from '../../../../shared/blade/blade-container-base';
import {BladeData} from '../../../../shared/blade/blade-data';
import {Blade} from '../../../../shared/blade/blade.model';
import {Command} from '../../../../shared/toolbar.service';
import {ModuleCodeService} from '../../../../service/module-code.service';
import {DialogResult} from '../../../../shared/dialog.service';
import {ModuleCode} from '../../../../entity/module-code.model';
import {Role} from '../../../../../../oldSrc/app/entity/role.model';

@Component({
  selector: 'app-module-code-list',
  templateUrl: './module-code-list.component.html',
  styleUrls: ['./module-code-list.component.css']
})
export class ModuleCodeListComponent extends BladeContainerBase<ModuleCode> {
  private originalTurb: ModuleCode[] = [];
  public moduleList: ModuleCode[] = [];


  constructor(
    private moduleCodeService: ModuleCodeService,
  ) {
    super();
  }

  // tslint:disable-next-line:use-life-cycle-interface
  ngOnInit(): void {
    this.commands = [
      new Command('add', '添加', 'fa fa-plus', 'setofkey:insert', blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
      new Command('edit', '编辑', 'fa fa-edit', 'setofkey:update', blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
      new Command('delete', '删除', 'fa fa-trash-o', 'setofkey:delete', blade => this.exeDelete(blade), blade => this.canDelete(blade))
    ];
    super.ngOnInit();
  }

  /**
   * 初始化数据
   *
   *
   * @memberof TurbstatusListComponent
   */
  initData(): any | Observable<any> | Promise<any> {
    this.moduleList = [];
    const moduleList = this.moduleCodeService.getBaiscModuleCode();
    Promise.all([moduleList])
      .then(res => {
        if (res[0] && res[1]) {
          this.originalTurb = [...res[0]];
          this.moduleList = [...this.moduleList];
        } else {
          throw new Error('查询失败！');
        }
      })
      .catch(error => this.bladeData.error = error)
      .then(res => this.bladeData.isLoading = false);
  }

  exeAdd(blend: Blade): void {
    const data: BladeData = new BladeData();
    data.id = 'moduleKeyCreate';
    data.title = 'KR分类';
    data.subtitle = '创建一个KR分类';
    data.isClosingDisabled = false;
    const createKeyBlade = this.bladeService.createBlade(ModuleCodeEditComponent, data, true);
    this.bladeService.showBlade(createKeyBlade, this.currentBlade);
  }

  canExeAdd(blend: Blade): boolean {
    return true;
  }

  /**
   * 单选
   *
   * @param {HTMLInputElement} event
   * @param {Role} key
   * @memberof RoleListComponent
   */
  onItemCheack(event: HTMLInputElement, key: Role) {
    key.isChecked = event.checked;
  }
  /**
   * 全选
   *
   * @param {HTMLInputElement} event
   * @memberof RoleListComponent
   */
  onAllCheack(event: HTMLInputElement) {
    if (this.moduleList.length > 0) {
      for (let i = 0; i < this.moduleList.length; i++) {
        this.moduleList[i].isChecked = event.checked;
      }
    }
  }

  exeEdit(blend: Blade): void {
    if (this.currentEntity != undefined) {

      const data: BladeData = new BladeData();
      data.id = 'moduleKeyEdit';
      data.title = this.currentEntity.name;
      const editKeyBlade = this.bladeService.createBlade(ModuleCodeEditComponent, data, false, this.currentEntity);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }

  canExeEdit(blend?: Blade): boolean {
    return this.currentEntity != null;
  }

  exeDelete(blend: Blade): void {
    this.dialogService
      .showConfirmationDialog('确认删除', '是否要删除选中项？')
      .subscribe(dialogResult => {
        if (dialogResult == DialogResult.Ok) {
          this.currentBlade.bladeData.isLoading = true;
          this.moduleCodeService.delete(this.currentEntity.id)
            .then(res => {
              if (res) {
                this.refresh(undefined);
              }
            })
            .catch(error => this.bladeData.error = error)
            .then(res => this.bladeData.isLoading = false);
        }
      });
  }

  canDelete(blend?: Blade): boolean {
    return this.currentEntity != null;
  }

  onSeletedChange(selectedKey: ModuleCode) {
    this.currentEntity = selectedKey;
  }
}
